package com.yanqu.road.server.cmd.activity.toyhouse;

import com.yanqu.road.entity.activity.toyhouse.ToyHouseUserData;
import com.yanqu.road.entity.activity.toyhouse.config.ToyHouseConfig;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.ToyHouseProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.toyhouse.ToyHouseModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.toyhouse.ToyHouseMgr;
import com.yanqu.road.server.pb.ToyHousePb;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

@Cmd(code = GameProtocol.S_TOY_HOUSE_ELIMINATE, desc = "消除方块")
public class ToyHouseEliminateCmd extends AbstractCommand {

    private int multi;
    private int fromX;
    private int fromY;
    private int toX;
    private int toY;

    private ToyHouseUserData userData = null;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        ToyHouseProto.ToyHouseEliminateReqMsg reqMsg = ToyHouseProto.ToyHouseEliminateReqMsg.parseFrom(packet.getBytes());
        multi = reqMsg.getMulti();
        fromX = reqMsg.getFromX();
        fromY = reqMsg.getFromY();
        toX = reqMsg.getToX();
        toY = reqMsg.getToY();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        // 活动配置
        ToyHouseConfig config = ToyHouseMgr.getConfig();
        if (config == null || !ActivityMgr.activityInTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_TOY_HOUSE_NO_ACTIVITY;
            return;
        }

        ret = player.getModule(ToyHouseModule.class).eliminateBlock(config, multi, fromX, fromY, toX, toY);
    }

    @Override
    public void sendData(GamePlayer player) {
        if (ret != 0) {
            ToyHouseProto.ToyHouseEliminateRespMsg.Builder builder = ToyHouseProto.ToyHouseEliminateRespMsg.newBuilder();
            builder.setRet(ret);
            if (userData != null && userData.getBlockDataMap().containsKey(multi)) {
                builder.setGameData(ToyHousePb.parseToyHouseGameDataMsg(userData, multi));
            }
            player.sendPacket(ClientProtocol.U_TOY_HOUSE_ELIMINATE, builder);
        }
    }
}
